SDE1 @ DP World
Bangalore, India
Mar 2025
₹32.5L + 4.5L Variable + 4L Other Bonus
Applied through company's job portal.
Coding Challenge
Flatten a nested JavaScript object into a key-value pair format.
Solution
let user = {
name: 'John',
address: {
country: 'India',
state: 'India',
education: {
school: "APS",
year: 2021
}
}
};
function flattenObject(obj = {}, parentKey = '') {
return Object.keys(obj).reduce((acc, key) => {
let updatedKey = parentKey ? `${parentKey}.${key}` : key;
if (typeof obj[key] === 'object' && obj[key] !== null) {
Object.assign(acc, flattenObject(obj[key], updatedKey));
} else {
acc[updatedKey] = obj[key];
}
return acc;
}, {});
}
let result = flattenObject(user, 'user');
console.log(result);
Output
{
'user.name': 'John',
'user.address.country': 'India',
'user.address.state': 'India',
'user.address.education.school': 'APS',
'user.address.education.year': 2021
}
Other Question Asked
Key Questions
useEffect, solved using the key prop.System Design - Button Component
const VARIANT = {
PRIMARY: 'PRIMARY',
SECONDARY: 'SECONDARY'
};
const Button = ({
text = '',
onClickHandler = () => {},
isLoading = false,
isDisabled = false,
variant = VARIANT.PRIMARY
}) => {
return (
<button
className={`button ${variant}`}
disabled={isDisabled || isLoading}
onClick={onClickHandler}
>
{text}
</button>
);
};
CSS Styles
.button.PRIMARY {
color: black;
background: white;
}
.button.SECONDARY {
color: white;
background: black;
}
Other Questions
Be the first to share your thoughts!
No comments yet.
Start the conversation!
Advertisement
Subscribe to FrontendGeek Hub for the frontend interview preparation, interview experiences, curated resources and roadmaps.
© 2024 FrontendGeek. All rights reserved